package com.duduli.li;

import com.duduli.li.po.Product;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetWebProduct {
    public List<Product> getProduct(String url){
        List<Product> list = new ArrayList<Product>();
        try {
            Document doc = Jsoup.connect(url).get();
            Elements listE = doc.getElementsByClass("gl-item");
            if(listE != null){
                for(Element ele : listE){
                    Product p = new Product();
                    Element e = ele.getElementsByClass("gl-i-wrap j-sku-item").first();
                    if(e != null){
//                        System.out.println(e);
                        if (!"".equals(e.attr("data-sku").trim())) {
                            p.setId(e.attr("data-sku"));
                        }
                        if (!"￥".equals(e.getElementsByTag("em").text())) {
                            p.setName((e.getElementsByTag("em").text()).replace("￥", ""));
                        }
                            p.setCategory(doc.title());
                        list.add(p);
                    }else {
                        continue;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
